package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Slf4j
@Service
public class DishServiceIml implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Transactional//多张表,需要开启事务
    public void addDish(DishDTO dishDTO) {
        //构造菜品基本数据，将其存入菜品表中
        Dish dish = new Dish();
        //拷贝属性值
        BeanUtils.copyProperties(dishDTO,dish);
        //调用mapper保存方法
        dishMapper.insert(dish);
        log.info("dishId={}",dishDTO.getId());
        //构造菜品口味数据，将其存入口味表中
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        //关联菜品id
        dishFlavorList.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        //调用mapper保存方法
        dishFlavorMapper.insertBatch(dishFlavorList);
    }
    /*
    * 菜品分页查询
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDto) {
        //1.设置分页参数
        //2.调用mapper的列表查询方法，强转为Page
        PageHelper.startPage(dishPageQueryDto.getPage(),dishPageQueryDto.getPageSize());
        Page<DishVO> dishList = dishMapper.list(dishPageQueryDto);
        log.info("菜品分页查询结果{}",dishList);
        //3.封装PageResult对象,返回结果
        return new PageResult(dishList.getTotal(),dishList.getResult());
    }
    /*
    * 批量删除菜品
     */
    @Override
    @Transactional//开启事务
    public void deleteById(List<Long> ids) {
        //1.删除菜品之前，先判断菜品是否起售
        ids.forEach(id -> {
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        //2.判断菜品是否被套餐关联
        Integer count = setmealDishMapper.countByDishId(ids);
        if(count > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //3.删除菜品基本信息dish表
        dishMapper.deleteByIds(ids);
        //4.删除菜品口味信息dish_flavor表
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO getById(Long id) {
        //创建DishVO对象
        DishVO dishVo = new DishVO();
        //1.根据菜品id查询菜品基本信息,封装到dishVo中
        Dish dish = dishMapper.selectById(id);
        //拷贝
        BeanUtils.copyProperties(dish,dishVo);
        //2.根据id查询菜品口味列表信息,封装到dishVo中
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishIds(id);
        dishVo.setFlavors(dishFlavors);
        //返回对象
        return dishVo;
    }
    /*
    * 修改菜品
     */
    @Transactional //开启事务
    public void update(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        //1.修改菜品的基本信息，dish表
        dishMapper.update(dish);
        //2.修改菜品口味，dish_flavor表
        //由于口味数据可能涉及增加，删除，修改，所以先全部删除旧删除，再添加
        dishFlavorMapper.deleteByIds(dto.getId());
        //获取新的口味数据
        List<DishFlavor> flavors = dto.getFlavors();
        //判断是否有口味数据
        if(flavors != null && flavors.size() > 0){
            //关联菜品id
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            //添加口味
            dishFlavorMapper.insertBatch(flavors);
        }
    }
}















